Reading Between the Lines: Distinguishing Macro Code from Open Code in Macros
نویسندگان
چکیده
Have you ever been confused by the sight of bad “SAS® grammar” such as statements that read %IF &X=A %THEN IF X=A...? Have you ever puzzled over how to iteratively generate DATA step DO loops or parts of them? Ever wonder why macros sometimes contain consecutive semicolons? If so, you are not alone. The macro facility enables us to generate open code that varies with circumstances, but of course the directions for code generation must be conveyed through more code. Inevitably, we find ourselves having to mix open code with macro code. Add to this the fact that macro code contains statements, keywords, and syntax that resemble their DATA step counterparts, and we begin to see the difficulties programmers experience with reading and writing macros. This paper attempts to clear up all the confusion by illustrating exactly what parts of a macro accept open code. Along with some supporting information such as macro syntax, quoting functions, and macro processing, we will discuss macros that only generate code (without any macro logic), as well as mixed macros or those that mix code generation with macro logic. Along the way comparisons are made to DATA step counterparts where applicable. Examples are provided to illustrate consequences of the rules such as bad SAS grammar and double semicolons. This paper is most useful to those beginning with macros and looking to solidify their current knowledge base of macros. INTRODUCTION Writing code is one thing, but writing code that writes code is quite another. For the most part, that is what we use macros for – providing instructions to the SAS system on how to generate code we want executed when called upon. Of course, code-generating instructions inevitably have to contain the code that is to be generated. Now that the obvious has been stated, let’s state the dilemma: how does the SAS system know which parts of what you are typing are instructions, and which parts are code to be generated? In order to begin talking about distinctions between open code and macro code, let’s first make sure we know the meanings of the two terms. Code itself is a language in which we provide instructions to the SAS system. The difference between open code and macro code is “to whom” the instructions are being provided and what those instructions entail. Open code is the code you write or the instructions you provide to the SAS system every day. When you write a PROC, you are telling the SAS system how to analyze a data set. A DATA step is a set of instructions for the SAS system (or the DATA step compiler) on how to build a data set. Certain DATA step instructions tell the SAS system how to set up the Program Data Vector while others instruct the SAS system how to populate variable values and ultimately generate observations for the new data set. Most open code (with the exception of a few isolated global statements such as TITLE, OPTIONS, and ODS) is organized into steps and the steps are run in the order in which they are found in the program. While open code is a set of instructions for processing data, macros are sets of instructions for processing, manipulating, and generating text. When a macro is called or invoked, the text generated becomes open code at the location of the call. When called inside a PROC or a DATA step, it is only after this execution of the macro does the step execute. The macro facility does not require macros to contain any macro logic. The instructions for generating text may only provide the text to be generated, or they may contain logic that determines how the text will be generated. At points in this paper, I refer to macros of the former type as trivial, because they do not take advantage of the power of the macro facility, and they accomplish nothing more than can be achieved by other means without the overhead of macro compiling. In spite of this, they will come up in examples because lessons can be learned from them, but most of the focus will be on what I call mixed macros. A mixed macro is one that uses macro logic to generate open code. The idea is that a piece of code has the potential to change with circumstances from one use of the program to the next, and so such macros are often defined with parameters that reflect these changing circumstances. Directions to the macro processor, including what text to generate, are often based on these parameters. The necessity to distinguish between these two languages comes from the simple fact that for macro code to generate open code, both must co-mingle in a macro. USE YOUR IMAGINATION Because of its very nature, writing any kind of code requires imagination. Take a DATA step for example. You can picture in your mind what you want your data set to look like, but your set of instructions are for a computer, and so you cannot just tell the computer verbally what you need or draw it a picture. You have to use code that the software Tutorials SUGI 31
منابع مشابه
Large Scale Standard Macros - A Methodical Approach to Development and Implementation
Much of the analysis for clinical trials in the pharmaceutical industry is done with very large standard macros. The use of these macros facilitates the goal of standardizing and accelerating the production of clinical reports within a company. Each standard macro is typically specialized to analyze and report on a specific type of data such as efficacy, safety, drug interactions, or demographi...
متن کاملTask Difficulty and Its Components: Are They Alike or Different across Different Macro-genres?
Task difficulty across different macro-genres continues to remain among less attended areas in second language development studies. This study examined the correlation between task difficulty across the descriptive, narrative, argumentative, and expository macro-genres. The three components of task difficulty (i.e., code complexity, cognitive complexity, and communicative stress) were also comp...
متن کاملComposable and Compilable Macros
Many macro systems, especially for Lisp and Scheme, allow macro transformers to perform general computation. Moreover, the language for implementing compile-time macro transformers is usually the same as the language for implementing run-time functions. As a side effect of this sharing, implementations tend to allow the mingling of compile-time values and run-time values, as well as values from...
متن کاملCode Compaction Using Genetic Algorithms
One method for compacting executable computer code is to replace commonly repeated sequences of instructions with macro instructions from a decoding dictionary. The size of the decoding dictionary is often small in comparison to the number of all possible macros. Choosing the macros that yield the best compaction is a di cult subset selection problem because multiple, but colliding, macros may ...
متن کاملZzw: a Macro Package for Books
The \noname package provides a powerful environment for writing macros. Its use makes macros easier to read, easier to write, and easier to document. It allows ready access to powerful control macros. It allows diagnostic and other code to be tagged for conditional inclusion. The \noname package is fully compatible with existing macros. Here are two major features. It allows easy access to arbi...
متن کامل